package servlet;

import it.polimi.swimv2.entities.HelpRequest;
import it.polimi.swimv2.exception.FeedbackException;
import it.polimi.swimv2.remote.FeedbackManagerRemote;
import it.polimi.swimv2.remote.HelpRequestManagerRemote;
import it.polimi.swimv2.util.RemoteBindingName;

import java.io.IOException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class FeedbackServlet
 */
public class FeedbackServlet extends HttpServlet {
	

//Jsp Pages
	private static final String FEEDBACK_FORM_JSP = "feedbackForm.jsp";
	private static final String ERROR_JSP = "error.jsp";
	private static final String INDEX_JSP = "index.jsp";
	private static final String LOGIN_FORM_JSP = "loginForm.jsp";
	
//Parameters of HTTP Request
	private static final String RQ_ID = "rqID";
	private static final String SCORE = "score";
	private static final String COMMENT = "comment";	
	private static final String SCORE_OUT_OF_RANGE = "scoreOutOfRange";
	private static final String SEND_FEEDBACK = "sendFeedback";
	private static final String FEEDBACK_FORM = "feedbackForm";
	private static final String USER = "user";
	private static final String ACT = "act";
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FeedbackServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		manageHttpRequest(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		manageHttpRequest(request, response);
	}

	/**
	 * manages the action requested by http request
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void manageHttpRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String act = request.getParameter(ACT);
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute(USER);
		if(username==null){
			response.sendRedirect(LOGIN_FORM_JSP);
			return;
		}
		if(act==null){
			response.sendRedirect(INDEX_JSP);
			return;
		}
		if(act.equals(FEEDBACK_FORM)){
			processShowFeedbackForm(request, response);
		}else if(act.equals(SEND_FEEDBACK)){
			processSendFeedback(request, response);
		}
		
		
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 * @throws FeedbackException
	 */
	private void processSendFeedback(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		try{
			FeedbackManagerRemote rem = retrieveFeedbackManagerRemote();
			String comment = request.getParameter(COMMENT);
			Integer score = Integer.parseInt(request.getParameter(SCORE));
			Integer rqID = Integer.parseInt(request.getParameter(RQ_ID));
			rem.sendFeedback(comment, score, rqID);
			RequestDispatcher dispatcher = request.getRequestDispatcher("HelpRequestServlet?act=showRequestDetails&rqID="+rqID);
			dispatcher.forward(request, response);		
		}catch(NamingException e){
			response.sendRedirect(ERROR_JSP);
		}catch(FeedbackException e) {
			request.setAttribute("error", SCORE_OUT_OF_RANGE );
			processShowFeedbackForm(request, response);					
		}
		
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void processShowFeedbackForm(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String rqID = request.getParameter(RQ_ID);	
		request.setAttribute(RQ_ID, rqID);
		RequestDispatcher dispatcher = request.getRequestDispatcher(FEEDBACK_FORM_JSP);
		dispatcher.forward(request, response);		
	}
	/**
	 * retrieves the remote Feedback object
	 * @return
	 * @throws NamingException
	 */
	private FeedbackManagerRemote retrieveFeedbackManagerRemote()
			throws NamingException {
		Context jndiContext = new InitialContext();
		Object ref = jndiContext.lookup(RemoteBindingName.FEEDBACK_MANAGER);
		FeedbackManagerRemote rem = (FeedbackManagerRemote) ref;
		return rem;
	}


}
